/******************************************************************************


******************************************************************************/
#include "sdkGlobal.h"
#include "sdkList.h"


static SdkSysList *AppMsgLink = NULL;


void sdkSysPostMessageInit(void)
{
    AppMsgLink = NULL;
}

/*******************************************************************

********************************************************************/
s32 sdkSysPostMessage(SDK_MSG *ppstMsg)
{
    if (AppMsgLink == NULL)
    {
        AppMsgLink = sdkListCreate(10, true);
        Assert(AppMsgLink != NULL);
    }

    if (ppstMsg != NULL && (AppMsgLink != NULL))
    {
        return sdkListAddAtEnd(AppMsgLink, ppstMsg);
    }
    return SDK_PARA_ERR;
}

/*******************************************************************

********************************************************************/
s32 sdkSysQueryMessage(SDK_MSG **pstMsg)
{
    SDK_MSG *pRet = (SDK_MSG *) sdkListGetAt(AppMsgLink, 0);

    if (pRet != NULL)
    {
        *pstMsg = pRet;
        Verify(sdkListRemoveData(AppMsgLink, pRet) >= 0);
        return SDK_OK;
    }
    return SDK_ERR;
}


/*******************************************************************

********************************************************************/
s32 sdkSysDestroyMsgList(void)
{
    SDK_MSG *p;

    while (sdkSysQueryMessage(&p) == SDK_OK)
    {
        sdkFreePersistentMem(p);
    }

    sdkListDestroy(AppMsgLink);
    AppMsgLink = NULL;
    return SDK_OK;
}


/*******************************************************************

********************************************************************/
s32 sdkSysPackMessage(pMSGFun pFun, u32 uiWParam, u32 uiLParam)
{
    typedef u8 (*ptempFun)(SDK_MSG *lpMsg);
    SDK_MSG *pMsg;

    if (pFun == NULL)
    {
        return SDK_PARA_ERR;
    }
    pMsg = (SDK_MSG *) sdkGetPersistentMem(sizeof(SDK_MSG));
    Assert(pMsg != NULL);
    memset(pMsg, 0, sizeof(SDK_MSG));
    pMsg->pFun = (ptempFun) pFun;
    pMsg->uiWParam = uiWParam;
    pMsg->uiLParam = uiLParam;
    return sdkSysPostMessage((SDK_MSG *) pMsg);
}

